import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/wait_task_count_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/wait_task_manager_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';

//任务待办
class ChildrenTabTwo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ChildrenTabTwoState();
  }
}

class ChildrenTabTwoState extends State<ChildrenTabTwo> {
  bool show = true;
  WaitTaskManagerDatas datas = WaitTaskManagerDatas();

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return LoadBuilder(
        visibility: show,
        builder: RefreshIndicator(
          child: ListView(
            children: <Widget>[
              ItemCounter("待审批抽检计划", datas.planstatisticscount, () {
                Navigator.pushNamed(context, "/waitApprovePlan");
              }, 0),
              ItemCounter("待完成检测任务", datas.completedtaskcount, () {
                Navigator.pushNamed(context, "/waitTaskList",
                    arguments: {"type": 0});
              }, 1),
              ItemCounter("待样品回库任务", datas.taskdealtwithcount, () {
                Navigator.pushNamed(context, "/waitTaskList",
                    arguments: {"type": 1});
              }, 1),
            ],
          ),
          onRefresh: getData,
        ));
  }

  Future getData() async {
    show = true;
    InternetAPI.getInstance()
        .get("/WTesting/DescSpotcheckTask/TaskToDoTestManagement", (data) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        setState(() {
          show = false;
          datas = WaitTaskManagerDatas.fromJson(data);
        });
      });
    }, params: {
      'warehouseid': DataSingleton().warehouseChoice.id,
      'tenantId': DataSingleton().Data.tenantId,
      "workuserid": DataSingleton().Data.id,
    });
  }
}

class ItemCounter extends StatelessWidget {
  String name;
  int counter;
  Function onTap;
  double top;

  ItemCounter(this.name, this.counter, this.onTap, this.top);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.all(12),
        margin: EdgeInsets.only(top: top),
        child: Row(
          children: <Widget>[
            Expanded(
                child: Text(
              name,
              style: TextStyle(color: Color(0xff323232), fontSize: 16),
            )),
            Text("【${counter}】",
                style: TextStyle(color: Color(0xff00A5F1), fontSize: 16)),
            Container(
              width: 16,
              height: 10,
              child: Image.asset("assets/images/arrow.png"),
            )
          ],
        ),
      ),
      onTap: onTap,
    );
  }
}
